{% extends "base.html" %}

{% block css %}
    <link href="{{ settings.MirrorSite }}/static/assets/google-code-prettify/desert.css" rel="stylesheet"/>
{% endblock %}

{% block script %}
    <script type="text/javascript" src="{{ settings.MirrorSite }}/static/assets/google-code-prettify/prettify.js"></script>
{% endblock %}

{% block body %}

{% if post.public or user.is_admin() %}

{% include "__post_script.html" %}
<script type="text/javascript">
    $(document).ready(function(){
        var $container = $(".container-list");

        $(".post", $container).each(function(){
            var $post = $(this);
            var post_id = get_post_id($post);

            var post_body = $("#post_body_"+post_id).val();

            $(".post-body", $post).html(md2html(post_body));
            $("pre>code", $post).parent().addClass("prettyprint");

            $(".cmdbar .comment", $post).click(function(){
                comment.show_comments($("#comments"), post_id);
            });
            $(".cmdbar .stats", $post).click(function(){
                var $this = $(this);
                var $icon = $this.find("i").clone();
                $.json_action("stats",{
                    stats_type: $.toJSON($this.attr("stats_type")),
                    id: $.toJSON(post_id),
                    oper: $.toJSON($this.attr("oper")),
                    name: $.toJSON($this.attr("name"))
                },function(resp){
                    $this.empty();
                    $this.append($icon);
                    $this.append(resp.stats[$this.attr("name")]);
                },function(err){
                    bootbox.alert(err);
                });
                $this.unbind("click");
            });

            $(".share", $post).click(function(){
                show_share({
                    id: post_id,
                    title: $(".title", $post).text(),
                    body: post_body
                });
            });

            $post.imagesLoaded(function(){
                bind_img_box($(".post-body", $post), "post_"+post_id, ".post-body");
                bind_img_info($(".post-body", $post), 150, 120);

                bind_img_box($(".post-thumbnails", $post), "thumbnails_"+post_id, ".post-thumbnails");
            });

            prettyPrint();
        });

        $(window).resize(function() {
            bind_img_info($(".post-body", $container), 150, 120);
        });

        {% if user.is_admin() %}
        var post_callback = function(post){
            editor.hide();
            window.location.href = "/post/"+post.id;
        };
        window.editor = new Editor(md_converter, post_callback);
        $("#new-post").click(function(){
            editor.new_post();
        });
        $(".menu-edit", $container).click(menu_edit);
        $(".menu-delete", $container).click(menu_delete);
        {% endif %}

        comment = new Comment(md2html);
        comment.show_comments($("#comments"), '{{ post.id }}');

    });
</script>
{% if user.is_admin() %}
{% include "_editor.html" %}
{% endif %}

<div class="row container-list">
  <div class="col-lg-offset-2 col-lg-8">
    <div class="post panel panel-default" id="post_{{ post.id }}">
        <input type="hidden" class="date_val" id="post_date_{{ post.id }}" value="{{ post.post_date }}" />
        <textarea class="dis-none body_val" id="post_body_{{ post.id }}">{{ post.body }}</textarea>
        <div class="panel-heading">
          <div class="post-title">
            <span class=""><img src="{{ post.author.avatar_url }}" class="avatar avatar32"/></span>
            {% if not post.public %}<span class="label label-default post-hide">{{ _("Hidden") }}</span>{% endif %}
            <a href="/post/{{ post.id }}" class="title">{{post.title}}</a>
            {% if user.is_admin() %}
            <div class="menu btn-group pull-right">
                <a class="dropdown-toggle" data-toggle="dropdown" href="">
                    <h4><i class="fa fa-toggle-down text-default"></i></h4>
                </a>
                <ul class="dropdown-menu">
                    <li><a class="menu-edit" href="javascript:;">{{ _("Edit") }}</a></li>
                    <li><a class="menu-delete" href="javascript:;">{{ _("Delete") }}</a></li>
                </ul>
            </div>
            {% endif %}
            <div class="pull-right">
                <div class="tags">
                    {% for tag in post.tags %}<a href="/tags/{{ tag }}">{{ tag }}</a>{% endfor %}
                </div>
            </div>
          </div>
        </div>
        <div class="panel-body">
            <div class="post-body">
            </div>
            {% if post.category.template == "Photo" %}
            <hr>
            <div class="post-thumbnails">
                {% for photo in post.photos %}
                    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
                        <div class="thumbnail">
                            <img src="{{ photo.url_thumb or photo.url }}" real_src="{{ photo.url }}" alt="{{ photo.alt }}" />
                        </div>
                    </div>
                {% endfor %}
            </div>
            {% endif %}
        </div>
        <div style="clear: both"></div>
        <div class="panel-footer">
            <small>
              <i class="fa fa-clock-o"></i> <span class="post-date">{{ post.post_date.strftime('%Y-%m-%d') }}</span>
              <span class="cmdbar pull-right">
                <i class="fa fa-user"></i> <span class="post-author">{{ post.author.nickname }}</span>&nbsp;
                <i class="fa fa-eye"></i> <span class="post-viewed">{{ post.stats.view_count }}</span>&nbsp;
                <button class="share btn btn-default btn-xs" title="{{ _("Share") }}">
                    <i class="fa fa-share"></i>{{ post.stats.share_count }}</button>
                <button class="stats btn btn-default btn-xs" stats_type="Post" name="like_count" oper="increase"
                    title="{{ _("Like") }}"><i class="fa fa-thumbs-o-up"></i>{{ post.stats.like_count }}
                </button>
                <button class="stats btn btn-default btn-xs" stats_type="Post" name="unlike_count" oper="increase"
                    title="{{ _("Unlike") }}"><i class="fa fa-thumbs-o-down"></i>{{ post.stats.unlike_count }}
                </button>
                <button class="comment btn btn-default btn-xs" title="{{ _("Comment") }}">
                    <i class="fa fa-comment-o"></i>{{ post.stats.comment_count }}
                </button>
              </span>
              <div class="clearfix"></div>
            </small>
        </div>
        <div id="comments" class="">
        </div>
    </div>

  </div>
</div>
{% include "__comment.html" %}

{% else %}
    <h1>{{ _("Error") }} {{ _("Page not found") }}</h1>
{% endif %}
{% endblock %}
